package com.hertz.alarm.dao;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
@Service
public class EhCacheDao {
  private static final Logger LOG = Logger.getLogger(EhCacheDao.class);
  @Resource
  public CacheManager cacheManager;
//  static {
//    cacheManager = CacheManager.create();
//  }

  public void save(String key, Object value, String region) {
    Cache cache = cacheManager.getCache(region);
    cache.put(new Element(key, value));
  }

  public void update(String key, Object value, String region) {
    Cache cache = cacheManager.getCache(region);
    cache.put(new Element(key, value));
  }

  public void remove(String key, String region) {
    Cache cache = cacheManager.getCache(region);
    cache.remove(key);
  }

  public Object get(String key, String region) {
    Cache cache = cacheManager.getCache(region);
    Element element = cache.get(key);
    if (element != null) {
      return element.getValue();
    } else {
      return null;
    }
  }

  public int getCacheSize(String region) {
    Cache cache = cacheManager.getCache(region);
    int elementsInMemory = cache.getSize();
    return elementsInMemory;
  }

  public void clear(String region) {
    Cache cache = cacheManager.getCache(region);
    cache.removeAll();
    LOG.error("============================" + region);
  }
  
  public List getKeys(String region){
    Cache cache = cacheManager.getCache(region);
    return cache.getKeys();
  }
}
